// ==UserScript==
// @name         ScienceDirect Ref
// @namespace    http://tampermonkey.net/
// @version      2024-03-14
// @description  try to take over the world!
// @author       You
// @match        https://www.sciencedirect.com/science/article/*
// @icon         https://www.sciencedirect.com/favicon.ico
// @grant        none

// ==/UserScript==

(function () {
  "use strict";
  var docu = document.documentElement;
  var $ = (s) => {
    return document.querySelector(s);
  };
  function nE(e, css = "", html = "") {
    var ele = document.createElement(e);
    css && (ele.style.cssText = css);
    html && (ele.innerHTML = html);
    return ele;
  }
  var ref, art;
  var div = nE("div");
  var itv = setInterval(() => {
    if (ref && art) {
      clearInterval(itv);
      art.append(div);
    }
    ref = $("ol.references");
    art = $("article");
  }, 2000);
  var sty = nE(
    "style",
    0,
    `
  .side-panel-reference{display:none;}
  div.accessbar-sticky {display:none;}
  div#gh-main-cnt{height:60px;}
  header#gh-cnt {position: sticky;
    left: 0;
    top: 0;
    box-shadow: 0 0 3px 1px #3338;
  }
  article {position: relative; width: 75% !important;}
  div#refdiv {
  position: absolute;
  z-index: 99999999;
  width: 500px;
  max-height: 200px;
  background: white;
  box-shadow: 1px 1px 4px 1px #3338;
  left: 400px;
  top: 500px;
  border-radius: 4px;
  overflow: scroll;
  display:none;
  }
  ul.references {
    margin-bottom: 0 !important;
}
ul.references+p{display:none;}
  `
  );
  docu.append(sty);
  var close = nE(
    "div",
    "position: absolute;right: 0;top: 0;cursor:pointer;",
    '<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5092" width="24" height="24"><path d="M512 570.88l196.864 196.8 58.88-58.88L570.752 512l196.864-196.864-58.816-58.88L512 453.248 315.136 256.32l-58.88 58.88L453.248 512l-196.864 196.864 58.88 58.88L512 570.752z" fill="#000000" fill-opacity=".9" p-id="5093"></path></svg>'
  );
  var ct = nE("div", "line-height: 20px;font-size: 16px;margin:8px;");
  div.id = "refdiv";
  close.onclick = () => {
    div.style.display = "none";
  };
  div.append(close);
  div.append(ct);
  docu.addEventListener("click", (ev) => {
    let e = ev.target;
    if (e.tagName === "SPAN") {
      e = e.parentElement;
    }
    if (e.tagName === "A" && e.name.slice(0, 2) === "bb") {
      let n = parseInt(e.name.replace(/[a-z]+/, ""));
      div.style.left = (ev.layerX < 620 ? ev.layerX : 620) + "px";
      div.style.top = ev.layerY + 16 + "px";
      if (e.name.at(2) === "i") {
        ct.innerHTML = ref.children[n].children[1].innerHTML;
      } else {
        setTimeout(() => {
          ct.innerHTML = $("div.side-panel-reference .side-panel-children").innerHTML;
        }, 100);
      }
      div.style.display = "block";
    }
  });
})();
